package com.qudian.pay.framework.util.mail;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class MailSenderUtil {
	
	private String emailAddress;
	
	private String emailPassword;
	
	private String mailServerHost;
	
	private String mailServerPort = "25";
	
	private boolean ssl;
	
	/**
	 * @Description: 发送html信息
	 * @param subject
	 * @param content
	 * @return void 返回类型
	 * @author gaoxiang
	 * @date 2015-7-22 下午3:39:02
	 */
	public void sendHtmlMail(String subject, String content, String[] toAddress) {
		MailSenderInfo mailInfo = setMailInfo(subject, content, toAddress);
		if (toAddress != null) {
			mailInfo.setSSL(ssl);
			SimpleMailSender.sendHtmlMail(mailInfo);
		}
	}
	
	/**
	 * @Description: 发送带图片的邮件
	 * @author gaoxiang
	 * @date 2017年12月26日 下午6:55:14
	 */
	public void sendHtmlMailWithPics(String subject, String content, String[] toAddress,Map<String,String> pics) {
		MailSenderInfo mailInfo = setMailInfo(subject, content, toAddress);
		mailInfo.setPics(pics);
		if (toAddress != null) {
			mailInfo.setSSL(ssl);
			SimpleMailSender.sendMessage(mailInfo);
		}
	}
	
	/**
	 * @Description: 带附件发送邮件
	 * @author gaoxiang
	 * @date 2017年1月3日 上午11:12:55
	 */
	public void sendHtmlMail(String subject, String content, String[] toAddress, Vector<String> attachName) {
		MailSenderInfo mailInfo = setMailInfo(subject, content,toAddress, attachName);
		if (toAddress != null) {
			mailInfo.setSSL(ssl);
			SimpleMailSender.sendHtmlMail(mailInfo);
		}
	}
	
	private MailSenderInfo setMailInfo(String subject, String content,String[] toAddress) {
		// 这个类主要是设置邮件
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost(mailServerHost);
		mailInfo.setMailServerPort(mailServerPort);
		mailInfo.setValidate(true);
		mailInfo.setUserName(emailAddress);
		mailInfo.setPassword(emailPassword);
		mailInfo.setFromAddress(emailAddress);
		mailInfo.setToAddress(toAddress);
		mailInfo.setSubject(subject);
		mailInfo.setContent(content);
		return mailInfo;
	}
	
	private MailSenderInfo setMailInfo(String subject, String content, String[] toAddress,Vector<String> attachName) {
		// 这个类主要是设置邮件
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost(mailServerHost);
		mailInfo.setMailServerPort(mailServerPort);
		mailInfo.setValidate(true);
		mailInfo.setUserName(emailAddress);
		mailInfo.setPassword(emailPassword);
		mailInfo.setFromAddress(emailAddress);
		mailInfo.setToAddress(toAddress);
		mailInfo.setSubject(subject);
		mailInfo.setContent(content);
		mailInfo.setAttachFileNames(attachName);
		return mailInfo;
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}
	
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
	public String getEmailPassword() {
		return emailPassword;
	}
	
	public void setEmailPassword(String emailPassword) {
		this.emailPassword = emailPassword;
	}
	
	public String getMailServerHost() {
		return mailServerHost;
	}
	
	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}
	
	public String getMailServerPort() {
		return mailServerPort;
	}
	
	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
	}
	
	public boolean isSsl() {
		return ssl;
	}
	
	public void setSsl(boolean ssl) {
		this.ssl = ssl;
	}
	
	public static void main(String args[]) {
		MailSenderUtil mailSenderUtil = new MailSenderUtil();
		mailSenderUtil.setMailServerHost("smtp.aliyun.com");
		mailSenderUtil.setMailServerPort("465");
		mailSenderUtil.setSsl(true);
		mailSenderUtil.setEmailAddress("geeaks@aliyun.com");
		mailSenderUtil.setEmailPassword("xxx");
		Map<String,String> pics = new HashMap<String,String>();
		pics.put("a", "E:/对比图.png");
		pics.put("b", "E:/2x.png");
		mailSenderUtil.sendHtmlMailWithPics("邮件标题", "邮件内容<img src='cid:a'><img src='cid:a'>", new String[] { "vsflyhigh@qq.com"},pics);
		System.out.println("发送完成");
	}
}
